# 列出所有可用的音频设备
import pyaudio


#1
def fun_a():
    p = pyaudio.PyAudio()
    info = p.get_host_api_info_by_index(0)
    numdevices = info.get('deviceCount')
    for i in range(0, numdevices):
            if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
                print ("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))

    """
    Input Device id  0  -  Microsoft 声音映射器 - Input
    Input Device id  1  -  麦克风 (Realtek(R) Audio)
    """

#2
# 获取设备数量
def fun_b():
    p = pyaudio.PyAudio()
    device_count = p.get_device_count()
    print(f"找到的设备数量: {device_count}")

    # 打印所有设备的名称和索引
    for i in range(device_count):
        device_info = p.get_device_info_by_index(i)
        print(f"设备索引: {i}, 设备名称: {device_info['name']}")



if __name__ == '__main__':
    fun_a()

